//
//  ByLogCollector.m
//  sensen_ios
//
//  Created by hebidu on 16/8/4.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByLogCollector.h"
#import <SystemConfiguration/CaptiveNetwork.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

@implementation ByLogCollector


//上报一键配置超时的日志
+(void)  uploadSmartConfigLog:(NSMutableDictionary *)dictionary{//errMsg
    DLog(@"上报一键配置超时的日志信息");
//    NSMutableDictionary * log = [[NSMutableDictionary alloc] init];
    
    //TODO: 收集用户手机信息（ios、android？ 品牌型号？android系统版本）
    //TODO: 收集用户网络信息（当前是4g?wifi? 网络类型可否收集到是 电信？移动？联通？根据IP？）
    //TODO: 收集app的版本号
    
    //SMARTCONFIG_TIMEOUT(一键配置) smartconfig
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setValue:@"smartconfig" forKey:@"type"];
    
    if (dictionary) {
        if (dictionary[@"did"]) {
            [dic setObject:dictionary[@"did"] forKey:@"did"];
        }
    }
    
    NSString *errMsg = dictionary[@"errMsg"];
    if (errMsg && errMsg.length > 0) {
        [dic setValue:errMsg forKey:@"log"];
    }else{
        [dic setValue:@"日志信息为空" forKey:@"log"];
    }
    [dic setValue:[self getInformation:dictionary] forKey:@"context"];

    [self uploadLog:dic];
    
}

//上报连接设备超市的日子
+(void) uploadLinkDeviceTimeoutLog:(NSMutableDictionary *)dictionary{
    DLog(@"上报设备连接超时的日志信息");
    
    //TODO: 收集用户手机信息（系统版本）
    //TODO: 收集用户网络信息（当前是4g?wifi? 网络类型可否收集到是 电信？移动？联通？根据IP？）
    
    //DEVICE_LINKING（设备连接监听时）devicelinking
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    
    if (dictionary) {
        if (dictionary[@"did"]) {
            [dic setObject:dictionary[@"did"] forKey:@"did"];
        }
    }

    
    
    [dic setValue:@"devicelinking" forKey:@"type"];
    [dic setValue:@"日志信息测试" forKey:@"log"];
    [dic setValue:[self getInformation:dictionary] forKey:@"context"];
    
    
    [self uploadLog:dic];
}

//相关信息获取
+ (NSString *)getInformation:(NSMutableDictionary *)dictionary{
    //手机版本信息
    NSString *version = [NSString deviceVersion];
    //手机类型
    NSString *type = @"iOS";
    
    
    //获取wifi
    NSString *wifiName = @"";
    NSString *wifi = @"";
    
    
    CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
    if (!wifiInterfaces) {
        NSLog(@"获取wifi_info失败");
    } else {
        NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
        for (NSString *interfaceName in interfaces) {
            CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
            if (dictRef) {
                NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
                NSLog(@"network info -> %@", networkInfo);
                wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];//Wi-Fi名字
                wifi = networkInfo[@"BSSID"];
                CFRelease(dictRef);
            }
        }
        
        CFRelease(wifiInterfaces);
    }
    
    
    
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    
    //设备信息
    if (dictionary) {
        if (dictionary[@"did"]) {
            [dic setObject:dictionary[@"did"] forKey:@"did"];
        }
        
        
        if (dictionary[@"password"]) {
             [dic setObject:dictionary[@"password"] forKey:@"password"];
        }
    }
    
    
    if (version) {
        [dic setObject:version forKey:@"version"];
    }
    [dic setObject:type forKey:@"type"];
    
    if (wifiName) {
        [dic setObject:wifiName forKey:@"wifiName"];
    }
    
    if (wifi) {
        [dic setObject:wifi forKey:@"wifi"];
    }
    
    [dic setObject:APP_VERSION forKey:@"appVersion"];
    
    
    //网络信息
    CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = [networkInfo subscriberCellularProvider];
    NSString *mobileNetworkCode = [carrier mobileNetworkCode];
    if (networkInfo.currentRadioAccessTechnology) {
        [dic setObject:[NSString stringWithFormat:@"%@",networkInfo.currentRadioAccessTechnology] forKey:@"currentRadioAccessTechnology"];
    }
    
    if (mobileNetworkCode) {
         [dic setObject:[NSString stringWithFormat:@"%@", mobileNetworkCode] forKey:@"mobileNetworkCode"];
    }else{
         [dic setObject:@"" forKey:@"mobileNetworkCode"];
    }
    
    NSLog(@"mobileNetworkCode = %@", mobileNetworkCode);
    NSLog(@"networkInfo = %@", networkInfo.currentRadioAccessTechnology);


    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:nil];
    NSString * jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}



//日志上传请求
+ (void)uploadLog:(NSMutableDictionary *)params{
    
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil];
    NSString * jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"jsonString = %@", jsonString);

    [BoyeHttpClient post:@"BY_Log_deviceLog" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error.domain] withDuration:1];
    }];

}




@end
